//
//  LBFixedPhotoCell.m
//  app
//
//  Created by 杨庆人 on 16/12/20.
//  Copyright © 2016年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBFixedPhotoCell.h"
#import <LBCategory/LBCategory.h>
#import <Masonry/Masonry.h>
#import <LBCoreKit/LBCoreKit.h>

@interface LBFixedPhotoCell ()


@property (nonatomic,strong)UIImageView * signImageView;
@property (nonatomic,strong)UIButton * deleButton;
@property (nonatomic,strong)UILabel * bottomLabel;
@property (nonatomic,strong)NSIndexPath * indexPath;
@property (nonatomic,copy)DeleButtonClickBlock deleBlock;

@end
@implementation LBFixedPhotoCell
- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.layer.borderWidth = 1;
        _bgImageView.layer.borderColor = [UIColor colorWithR:218 G:218 B:218 A:1].CGColor;
        _bgImageView.layer.cornerRadius = 10;
        _bgImageView.contentMode = UIViewContentModeScaleAspectFill;
        _bgImageView.clipsToBounds = YES;
        _bgImageView.hidden = YES;
    }
    return _bgImageView;
}

- (UIImageView *)signImageView {
    if (!_signImageView) {
        _signImageView = [[UIImageView alloc] init];
        _signImageView.image = [UIImage imageNamed:@"LBFormView.bundle/gray+"];
        _signImageView.hidden = YES;
    }
    return _signImageView;
}
- (UIButton *)deleButton{
    if (!_deleButton) {
        _deleButton = ({
            UIButton * button  = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setBackgroundImage:[UIImage imageNamed:@"LBFormView.bundle/dele_bule"] forState:UIControlStateNormal];
            button.hidden = YES;
            [button addTarget:self action:@selector(deleButtonClick) forControlEvents:UIControlEventTouchUpInside];
            
            button;
        });
    }
    return _deleButton;
}
- (UILabel *)bottomLabel{
    if (!_bottomLabel) {
        _bottomLabel = [[UILabel alloc] init];
        _bottomLabel.font = [UIFont systemFontOfSize:14];
        _bottomLabel.numberOfLines = 0;
        _bottomLabel.textAlignment = NSTextAlignmentCenter;
        _bottomLabel.adjustsFontSizeToFitWidth = YES;
        _bottomLabel.hidden = YES;
        _bottomLabel.textColor = [UIColor colorWithR:134 G:134 B:134 A:1];
        _bottomLabel.text = @"标题";
    }
    return _bottomLabel;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initSubViews];
    }
    return self;
}

- (void)layoutSubviews {
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_equalTo(self);
        make.top.mas_equalTo(self).offset(11);
        make.right.mas_equalTo(self).offset(-11);
    }];
    [self.deleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.mas_equalTo(self);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    [self.signImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(32, 32));
        make.bottom.mas_equalTo(self.mas_centerY).offset(8);
        make.centerX.mas_equalTo(self.bgImageView);
    }];
    [self.bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bgImageView).offset(5);
        make.right.mas_equalTo(self.bgImageView).offset(-5);
        make.top.mas_equalTo(self.mas_centerY).offset(8);
        make.bottom.mas_equalTo(self.bgImageView.mas_bottom).offset(-5);
    }];
}
- (void)initSubViews{
    [self addSubview:self.bgImageView];
    [self addSubview:self.deleButton];
    [self addSubview:self.signImageView];
    [self addSubview:self.bottomLabel];
}

- (void)setCellWithUploadPhotoModel:(LBUploadPhotoModel *)model itemName:(NSString *)itemName indexPath:(NSIndexPath *)indexPath photoCellType:(LBUploadPhotoCellType)photoCellType deleBlock:(DeleButtonClickBlock)deleBlock{

    __weak typeof(self)weakSelf = self;
    self.indexPath = indexPath;
    self.bottomLabel.text = itemName;
    self.deleBlock = deleBlock;
    
    if (model.image) {
        
        UIImage * newImage = [self imageWithImageSimple:model.image scaledToSize:CGSizeMake(model.image.size.width, model.image.size.height)];
        
        while (newImage.size.width > [[UIScreen mainScreen] bounds].size.width) {
            newImage = [self imageWithImageSimple:newImage scaledToSize:CGSizeMake(newImage.size.width * 0.5, newImage.size.height * 0.5)];
        }
        
        self.bgImageView.image = newImage;
        
        [self reloadFinshWithModel:model];
    }
    else {
        [self reloadNoImageUrlWithIndexPath:indexPath];
        [self.bgImageView sd_setImageWithURL:LBIMAGEURL(model.urlString) placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            if (model.urlString.length || model.objectKey.length) {
                [weakSelf reloadFinshWithModel:model];
            }
            else{
                [weakSelf reloadNoImageUrlWithIndexPath:indexPath];
            }
        }];
    }
}




- (void)deleButtonClick{
    !self.deleBlock ? :self.deleBlock(self.indexPath);
}

// 加载带URl的cell
- (void)reloadFinshWithModel:(LBUploadPhotoModel *)model{

    self.deleButton.hidden = NO;
    self.signImageView.hidden = YES;
    self.bottomLabel.hidden = YES;
    self.bgImageView.hidden = NO;
}

// 加载不带URl的cell
- (void)reloadNoImageUrlWithIndexPath:(NSIndexPath *)indexPath{
    
    self.deleButton.hidden = YES;
    self.signImageView.hidden = NO;
    self.bottomLabel.hidden = NO;
    self.bgImageView.hidden = NO;
}
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
@end
